import router from './router'
import store from './store'
import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { getToken } from '@/utils/auth' // get token from cookie
import getPageTitle from '@/utils/get-page-title'
import { axios_get } from '@/views/axios_api/http';

NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ['/login'] // no redirect whitelist

router.beforeEach(async(to, from, next) => {
  // start progress bar
  // NProgress.start()

  // set page title
  // document.title = getPageTitle(to.meta.title)

  // determine whether the user has logged in
  const hasToken = getToken()
  if(!hasToken){
    console.log('未登录，跳转至登录页面')
    if (to.path === '/login'){
    next()
    }else{
      next('login')
    }
  }else{
    // 在登录页面
    if (to.path === '/login'){
      console.log('您已登陆')
      next('/dashboard/homepage')
    }else{
      axios_get('right_judge/', {url:to.fullPath}).then(resp=>{
        if(!resp.data){
          if(to.path == '/403' || to.path == '/dash_board'){
            next()
            return false
          }
          next('/403')
          return false
        }
      })
      // 正常操作
      next()
    }
  }
})

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
